(function(){
    window.beans=window.beans||{};
    beans.util=beans.util||{};
    /**
     *  Add function to get Detail from server;
     *  */
    function jsonAjax(url, param, callBack,lazyTime) {
    	for ( i in param) {
    		if(typeof param[i]=='object'){
    			param[i]=JSON.stringify(param[i]);
    		}
    		if(typeof param[i]=='function'){
    			delete param[i];
    		}
		}
    	var aparam={
                url : url,
                type : "POST",
                dataType : "json",
                data : param,
                async : true,
                success : callBack,
                error : function(data) {
                }
            };
    	if(lazyTime&&lazyTime>0){
    		FeishiUtil.lazyAjax(aparam,lazyTime);
    	}else{
    		FeishiUtil.ajax(aparam);
    	}
    }
    function checkedJsonAjax(url, param, callBack,resultKey,lazyTime){
        var newCallBack=function(data){
            if (data.errorCode == '0x0') {
            	if(resultKey=="errorInfo"){
            		var result=data[resultKey];
            		try{
            			result=JSON.parse(result);
            		}catch(e){}
            		callBack(result);
            	}else{
            		callBack(data[resultKey]);
            	}
            } else {
                console.log(data.errorInfo);
            }
        };
        jsonAjax(url,param,newCallBack,lazyTime);
    }
    function jsonp(url,param,callBack,error){
    	$.ajax({
    		   url: url,
    		   type: "GET",
    		   dataType: 'jsonp',
    		   jsonp: 'jsoncallback',
    		   data: param,
    		   timeout: 5000,
    		   success: callBack,
    		   error:error
    		});
    }
    function ejsonAjax(url, param, callBack,lazyTime) {
    	for ( i in param) {
    		if(typeof param[i]=='object'){
    			param[i]=JSON.stringify(param[i]);
    		}
    		if(typeof param[i]=='function'){
    			delete param[i];
    		}
		}
    	var rparam={
                url : url,
                type : "POST",
                dataType : "text",
                data : getEparam(param),
                async : true,
                success : getEcallBack(callBack),
                error : function(data) {
                }
            };
//    	console.log(param,getEparam(param));
    	if(lazyTime&&lazyTime>0){
    		FeishiUtil.lazyAjax(rparam,lazyTime);
    	}else{
    		FeishiUtil.ajax(rparam);
    	}
    }
    function ejsonp(url,param,callBack,error){
    	$.ajax({
    		url: url,
    		type: "GET",
    		dataType: 'jsonp',
    		jsonp: 'jsoncallback',
    		data: getEparam(param),
    		timeout: 5000,
    		success: getEcallBack(callBack),
    		error:error
    	});
    }
    function getEparam(param){
    	return {
    		"requestType":"encode",
    		"data":encode(param)}
    }
    function getEcallBack(callBack){
    	return function(data){
    		var newData=data&&JSON.parse(ByteUtil.decode64(data,location.origin+location.pathname));
    		if(typeof callBack=='function'){
    			callBack(newData);
    		}
    	}
    }
    function encode(param){
    	var key="";
    	var pwd=location.origin+location.pathname;
    	if(typeof param=='string'){
    		key=param;
    	}
    	if(typeof param=='object'){
    		key=JSON.stringify(param);
    	}
    	return ByteUtil.encode64(key,pwd);
    }
    beans.util.getEparam=getEparam;
    beans.util.getEcallBack=getEcallBack;
    
    beans.util.checkedJsonAjax=checkedJsonAjax;
    beans.util.jsonAjax=jsonAjax;
    beans.util.jsonp=jsonp;
    beans.util.ejsonAjax=ejsonAjax;
    beans.util.ejsonp=ejsonp;
})();